﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSL.AjaxResult.KaiJiang;
using System.Data;

namespace TSL.AjaxResult
{
    public class OpenCode
    {
        [TSL.Utility.jQueryInvokeMethod()]
        public KaiJiangResult GetOpenCode()
        {
            List<TSL.Model.KaiJiang> list = new List<TSL.Model.KaiJiang>();
            TSL.BLL.OpenCodes dals = new TSL.BLL.OpenCodes();

            DataTable dt = new TSL.BLL.OpenCodes().GetSscCodes("state=100").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string codes = dt.Rows[i]["num_info"].ToString().Trim();
                int sscid = Convert.ToInt32(dt.Rows[i]["ssc_id"].ToString().Trim());
                string lottery_num = dt.Rows[i]["lottery_num"].ToString().Trim();

                DataTable BuySSC_DT = new TSL.BLL.OpenCodes().GetListOpenCodes(string.Concat(new object[] { "ssc_id='", sscid, "' and expect='", lottery_num, "' and state=6100" })).Tables[0];
                for (int j = 0; j < BuySSC_DT.Rows.Count; j++)
                {
                    TSL.Model.BuySSCDT buyssc = RowBind(BuySSC_DT.Rows[j]);

                    if ((Convert.ToInt32(buyssc.usertype) == 3001) || (Convert.ToInt32(buyssc.usertype) == 3002))
                    {
                        dals.MyFanDian(buyssc.username, buyssc.lotteryid, buyssc.protype, buyssc.expect, buyssc.allmoney);
                    }
                    this.FanDian(buyssc.username + "&" + buyssc.regfrom, buyssc.lotteryid, buyssc.protype, buyssc.expect, buyssc.allmoney);

                    TSL.Model.KaiJiang kaijiang = this.Open(buyssc, codes);
                    list.Add(kaijiang);
                }
                dals.CloseCaipiao(sscid, lottery_num);
            }

            if (list.Count == 0)
            {
                return new KaiJiangResult(-1, "无数据");
            }

            KaiJiangResult kaijiangres = new KaiJiangResult();
            kaijiangres.list = list;
            return kaijiangres;
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public KaiJiangResult GetOpenCodeForDanHao(string DanHao)
        {
            if (string.IsNullOrEmpty(DanHao) || DanHao.Length < 5)
            {
                 return new KaiJiangResult(-1, "无数据");
            }

            List<TSL.Model.KaiJiang> list = new List<TSL.Model.KaiJiang>();
            TSL.BLL.OpenCodes dals = new TSL.BLL.OpenCodes();

            DataTable BuySSC_DT = new TSL.BLL.OpenCodes().GetListOpenCodes("lotteryid like '%" + DanHao + "'").Tables[0];
            if (BuySSC_DT.Rows.Count > 0)
            {
                TSL.Model.BuySSCDT buyssc = RowBind(BuySSC_DT.Rows[0]);

                DataTable dt = new TSL.BLL.OpenCodes().GetSscCodes(string.Concat(new object[] { "ssc_id='", buyssc.ssc_id, "' and lottery_num='", buyssc.expect, "'" })).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    string codes = dt.Rows[0]["num_info"].ToString().Trim();

                    if ((Convert.ToInt32(buyssc.usertype) == 3001) || (Convert.ToInt32(buyssc.usertype) == 3002))
                    {
                        dals.MyFanDian(buyssc.username, buyssc.lotteryid, buyssc.protype, buyssc.expect, buyssc.allmoney);
                    }
                    
                    this.FanDian(buyssc.username + "&" + buyssc.regfrom, buyssc.lotteryid, buyssc.protype, buyssc.expect, buyssc.allmoney);
                    TSL.Model.KaiJiang kaijiang = this.Open(buyssc, codes);
                    list.Add(kaijiang);
                }
            }

            if (list.Count == 0)
            {
                return new KaiJiangResult(-1, "无数据");
            }

            KaiJiangResult kaijiangres = new KaiJiangResult();
            kaijiangres.list = list;
            return kaijiangres;
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public KaiJiangResult GetOpenCodeForQiHao(string ssc_id, string qihao)
        {
            if (string.IsNullOrEmpty(ssc_id) || string.IsNullOrEmpty(qihao))
            {
                return new KaiJiangResult(-1, "无数据");
            }

            List<TSL.Model.KaiJiang> list = new List<TSL.Model.KaiJiang>();
            TSL.BLL.OpenCodes dals = new TSL.BLL.OpenCodes();

            DataTable dt = new TSL.BLL.OpenCodes().GetSscCodes("ssc_id='" + ssc_id + "' and lottery_num='" + qihao + "'").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string codes = dt.Rows[i]["num_info"].ToString().Trim();
                int sscid = Convert.ToInt32(dt.Rows[i]["ssc_id"].ToString().Trim());
                string lottery_num = dt.Rows[i]["lottery_num"].ToString().Trim();
                DataTable BuySSC_DT = new TSL.BLL.OpenCodes().GetListOpenCodes(string.Concat(new object[] { "ssc_id='", sscid, "' and expect='", lottery_num, "' and state=6100" })).Tables[0];
                for (int j = 0; j < BuySSC_DT.Rows.Count; j++)
                {
                    TSL.Model.BuySSCDT buyssc = RowBind(BuySSC_DT.Rows[j]);

                    if ((Convert.ToInt32(buyssc.usertype) == 3001) || (Convert.ToInt32(buyssc.usertype) == 3002))
                    {
                        dals.MyFanDian(buyssc.username, buyssc.lotteryid, buyssc.protype, buyssc.expect, buyssc.allmoney);
                    }
                    this.FanDian(buyssc.username + "&" + buyssc.regfrom, buyssc.lotteryid, buyssc.protype, buyssc.expect, buyssc.allmoney);

                    TSL.Model.KaiJiang kaijiang = this.Open(buyssc, codes);
                    list.Add(kaijiang);
                }
                dals.CloseCaipiao(sscid, lottery_num);
            }

            if (list.Count == 0)
            {
                return new KaiJiangResult(-1, "无数据");
            }

            KaiJiangResult kaijiangres = new KaiJiangResult();
            kaijiangres.list = list;
            return kaijiangres;
        }

        protected TSL.Model.BuySSCDT RowBind(DataRow row)
        {
            TSL.Model.BuySSCDT buyssc = new Model.BuySSCDT();
            buyssc.ssc_id = Convert.ToInt32(row["ssc_id"].ToString().Trim());
            buyssc.username = row["username"].ToString().Trim();
            buyssc.ssc_name = row["ssc_name"].ToString().Trim();
            buyssc.caipiao_name = row["caipiao_name"].ToString().Trim();
            buyssc.protype = row["protype"].ToString().Trim();
            buyssc.expect = row["expect"].ToString().Trim();
            buyssc.lotteryid = row["lotteryid"].ToString().Trim();
            buyssc.mycodes = row["codes"].ToString().Trim();
            buyssc.bet = Convert.ToDouble(row["betmenu"].ToString().Trim());
            buyssc.onemoney = Convert.ToDouble(row["onemoney"].ToString().Trim());
            buyssc.fdnum = Convert.ToInt32(row["fdnum"].ToString().Trim());
            buyssc.regfrom = row["regfrom"].ToString().Trim();
            buyssc.allmoney = Convert.ToDouble(row["allmoney"].ToString().Trim());
            buyssc.beishu = Convert.ToDouble(row["beishu"].ToString().Trim());
            buyssc.usertype = row["usertype"].ToString().Trim();
            buyssc.rate = Convert.ToDouble(row["rate_dw"].ToString().Trim());
            buyssc.weishu = Convert.ToInt32(row["weishu"].ToString().Trim());
            buyssc.Factor = Convert.ToDouble(row["Factor"].ToString().Trim());
            return buyssc;
        }

        protected TSL.Model.KaiJiang Open(TSL.Model.BuySSCDT buyssc, string codes)
        {
            string username = buyssc.username;
            int sscid = buyssc.ssc_id;
            string ssc_name = buyssc.ssc_name;
            string caipiao_name = buyssc.caipiao_name;
            string protype = buyssc.protype;
            string expect = buyssc.expect;
            string lottery_id = buyssc.lotteryid;
            string buycodes = buyssc.mycodes;
            double bet = buyssc.bet;
            double beishu = buyssc.beishu;
            double onemoney = buyssc.onemoney;
            int fdnum = buyssc.fdnum;
            double rate = buyssc.rate;
            int weishu = buyssc.weishu;
            double Factor = buyssc.Factor;

            int winbonus = 0;
            double winmoney = 0.0;
            TSL.BLL.OpenCodes dals = new TSL.BLL.OpenCodes();
            TSL.Model.OpenCodes model = new TSL.Model.OpenCodes();
            TSL.Model.webconfig wb = new TSL.BLL.webconfig().GetModel();
            switch (sscid)
            {
                case 100001:
                    winbonus = KaiJiang1.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100002:
                    winbonus = KaiJiang1.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100024:
                    winbonus = KaiJiang1.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100025:
                    winbonus = KaiJiang2.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100026:
                    winbonus = KaiJiang2.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100027:
                    winbonus = KaiJiang2.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100028:
                    winbonus = KaiJiang2.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100029:
                    winbonus = KaiJiang3.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100030:
                    winbonus = KaiJiang6.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100031:
                    winbonus = KaiJiang4.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100032:
                    winbonus = KaiJiang5.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100033:
                    winbonus = KaiJiang5.OpenJJ(caipiao_name, codes, buycodes);
                    break;

                case 100034:
                    winbonus = KaiJiang5.OpenJJ(caipiao_name, codes, buycodes);
                    break;
            }

            TSL.Model.KaiJiang kaijiang = new TSL.Model.KaiJiang();
            kaijiang.lottery_id = lottery_id;
            kaijiang.username = username;
            kaijiang.caipiao_name = caipiao_name;
            kaijiang.expect = expect;
            kaijiang.winbonus = winbonus;
            kaijiang.winmoney = 0;
            kaijiang.state = "未中奖";

            if (winbonus > 0)
            {
                winmoney = Math.Round((double)(((bet + (fdnum * Factor)) * ((onemoney * beishu) / 2.0)) * winbonus), 2);
                if (Convert.ToDouble(wb.max_jiangjin.ToString()) < winmoney)
                {
                    winmoney = Convert.ToDouble(wb.max_jiangjin.ToString());
                }
                dals.OpenCode(username, winbonus, lottery_id, expect, protype, winmoney);
                kaijiang.winmoney = winmoney;
                kaijiang.state = "已中奖";
                return kaijiang;
            }
            dals.UnWin(username, lottery_id, expect);
            return kaijiang;
        }


        public void FanDian(string regfrom, string lotteryid, string protype, string expect, double allmoney)
        {
            TSL.BLL.OpenCodes dals = new TSL.BLL.OpenCodes();
            string username = string.Empty;
            string xusername = string.Empty;
            regfrom = regfrom.Replace("&&", "&");
            string[] user = regfrom.Split(new char[] { '&' });
            for (int i = 0; i < (user.Length - 1); i++)
            {
                xusername = user[i].Replace("&", "").ToString().Trim();
                username = user[i + 1].Replace("&", "").ToString().Trim();
                if (username != "")
                {
                    dals.FanDian(username, xusername, lotteryid, expect, protype, allmoney);
                }
            }
        }
    }
}
